﻿/*
 * @author Rui Zhong, 依赖jquery, jquery.form.js, layer.js
 */
; (function ($) {
    $.recoobHelper = {
        //显示等待窗口
        showLoading: function () {
            this._loading = layer.load(1, {
                shade: [0.1, '#fff'] //0.1透明度的白色背景
            });
        },
        //关闭等待窗口
        closeLoading: function () {
            layer.close(this._loading);
        },
        //弹出消息提示
        showMessage: function (msgStr) {
            this._message = layer.msg(msgStr);
        },
        //关闭消息提示
        closeMessage: function () {
            layer.msg(this._message);
        },
        //确认弹窗
        confirm: function (text, confirmCallback, confirmText, cancelText) {
            if (!confirmText) {
                confirmText = "确定";
            }
            if (!cancelText) {
                cancelText = "取消";
            }
            layer.confirm(text, { btn: [confirmText, cancelText] }, confirmCallback);
        },
        //警告弹窗
        alert: function (text, confirmCallback) {
            layer.confirm(text, { btn: ["确定"] }, confirmCallback);
        },
        //处理服务端定义的AjaxResult类
        ajaxResultCallback: function (result) {
            if (result.Messages && result.Messages instanceof Array) {
                if (result.Success === true) {
                    var msgStr = "";
                    for (var i = 0 ; i < result.Messages.length; i++) {
                        if (i !== 0) {
                            msgStr += "<br/>";
                        }
                        msgStr += result.Messages[i]
                    }
                    msgStr = $.trim(msgStr);
                    if (msgStr) {
                        $.recoobHelper.showMessage(msgStr);
                    }
                }
                else {
                    var msgStr = "";
                    for (var i = 0 ; i < result.Messages.length; i++) {
                        if (i !== 0) {
                            msgStr += "<br/>";
                        }
                        msgStr += result.Messages[i]
                    }
                    msgStr = $.trim(msgStr);
                    if (msgStr) {
                        $.recoobHelper.showMessage(msgStr);
                    }
                }
            }

            var redirectUrl = "";
            if (result.ReloadPage) {
                redirectUrl = window.location.href;
            }
            else if (result.RedirectUrl) {
                redirectUrl = result.RedirectUrl;
            }

            if (redirectUrl) {
                setTimeout(function () {
                    window.location.href = redirectUrl;
                }, 500);
            }
        },
        //ajax方式提交表单，简单包装了一层jquery.form.js
        sendAjaxForm: function ($form, extraData, callback, showLoading) {
            if (showLoading === undefined || showLoading) {
                this.showLoading();//loading效果
            }
            $form.ajaxSubmit({
                //timeout: 10000,
                data: extraData,
                error: function () {
                    if (showLoading === undefined || showLoading) {
                        $.recoobHelper.closeLoading();
                    }
                    $.recoobHelper.showMessage("系统似乎暂时出现了错误~")
                },
                success: function (result) {
                    if (showLoading === undefined || showLoading) {
                        $.recoobHelper.closeLoading();
                    }
                    $.recoobHelper.ajaxResultCallback(result);
                    //回调函数
                    if (callback && callback instanceof Function) {
                        callback(result);
                    }
                }
            });
        },
        //扩展的ajax函数
        send: function (method, url, data, callback, ajaxExtra, showLoading) {
            showLoading = showLoading === undefined || showLoading;
            if (showLoading) {
                $.recoobHelper.showLoading();//loading效果
            }
            if (method) {
                method = method.toUpperCase();
            }

            var ajaxOptions = {
                type: method,
                url: url,
                dataType: "json",
                data: data,
                //timeout: 10000,
                error: function () {
                    if (showLoading) {
                        $.recoobHelper.closeLoading();
                    }
                    $.recoobHelper.showMessage("系统似乎暂时出现了错误~")
                },
                success: function (result) {
                    if (showLoading) {
                        $.recoobHelper.closeLoading();
                    }
                    if (ajaxExtra && ajaxExtra.mimeType) {
                        result = JSON.parse(result);
                    }

                    $.recoobHelper.ajaxResultCallback(result);

                    //回调函数
                    if (callback && callback instanceof Function) {
                        callback(result);
                    }
                }
            };

            if (ajaxExtra) {
                ajaxOptions = $.extend(ajaxOptions, ajaxExtra);
            }

            $.ajax(ajaxOptions);
        },
        //发送post请求
        post: function (url, data, callback, ajaxExtra, showLoading) {
            this.send("POST", url, data, callback, ajaxExtra, showLoading);
        },
        //发送get请求
        get: function (url, data, callback, ajaxExtra, showLoading) {
            this.send("GET", url, data, callback, ajaxExtra, showLoading);
        }
    };
})(jQuery);